package GUI;

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JTextField;
/**
 * Managing the card by changing the status to freeze/ok/graduate for specific users
 * @author Lidor
 *
 */
public class CardManagingGUI extends JPanel {

	public MainFrame mf;
    public DefaultTableModel freezeTableModel;
    private JScrollPane scrollPane;
    public JTable table;
    public DefaultTableModel unfreezeTableModel;
    private JScrollPane scrollPane2;
    public JTable table2;
    private JTextField textUserID;
    private JTextField textGraduatedUser;
    
	/**
	 * initialize all the text fields
	 */
    public void init()
    {
    	textGraduatedUser.setText("");
    	textUserID.setText("");
    	for(int i=0;i<freezeTableModel.getRowCount();i++)
    		freezeTableModel.removeRow(i);
    	
    	for(int i=0;i<unfreezeTableModel.getRowCount();i++)
    		unfreezeTableModel.removeRow(i);

    }
	/**
	 * Create the panel.
	 * @param M main frame of the program
	 */
	public CardManagingGUI(MainFrame M) {
		setPreferredSize(new Dimension(600, 520));
		setMaximumSize(new Dimension(600, 520));
		setMinimumSize(new Dimension(600, 520));
		setLayout(null);
		this.mf=M;
		
		JLabel lblCardManagingSystem = new JLabel("Card Managing System");
		lblCardManagingSystem.setFont(new Font("Segoe UI", Font.BOLD, 16));
		lblCardManagingSystem.setBounds(205, 12, 225, 22);
		add(lblCardManagingSystem);
		
		JLabel lblUserForUnfreeze = new JLabel("Users for unfreeze:");
		lblUserForUnfreeze.setBounds(31, 238, 136, 14);
		add(lblUserForUnfreeze);
		
		freezeTableModel= new DefaultTableModel();
        freezeTableModel.addColumn("UserID");
        freezeTableModel.addColumn("CopyID");
        freezeTableModel.addColumn("Return Date");
        freezeTableModel.addColumn("Today Date");
        
        scrollPane = new JScrollPane();
        scrollPane.setBounds(177, 45, 290, 129);
        add(scrollPane);
        
        table= new JTable(freezeTableModel){
                
                public boolean isCellEditable(int rowIndex, int colIndex) {
                          return false; //Disallow the editing of any cell
                          }
                          };
                
        
        scrollPane.setViewportView(table);
        table.setRowSelectionAllowed(true);
        table.setVisible(true);
        
        unfreezeTableModel= new DefaultTableModel();
        unfreezeTableModel.addColumn("UserID");
        unfreezeTableModel.addColumn("Card Status");

        
        scrollPane2 = new JScrollPane();
        scrollPane2.setBounds(177, 185, 290, 129);
        add(scrollPane2);
        
        table2= new JTable(unfreezeTableModel){
                
                public boolean isCellEditable(int rowIndex, int colIndex) {
                          return false; //Disallow the editing of any cell
                          }
                          };
                
        
        scrollPane2.setViewportView(table2);
        table2.setRowSelectionAllowed(true);
        table2.setVisible(true);
        
        
        
		
        ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		
		JLabel lblUserForFreeze = new JLabel("Users for freeze:");
		lblUserForFreeze.setBounds(31, 99, 110, 15);
		add(lblUserForFreeze);
		
		JButton btnFreezeAll = new JButton("Freeze All");
		btnFreezeAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Object> send = new ArrayList<>();
				send.add("3");
				send.add("FreezeAll");
				mf.client.accept(send);
			}
		});
		btnFreezeAll.setBounds(495, 95, 95, 23);
		add(btnFreezeAll);
		
		JButton btnUnfreeze = new JButton("Unfreeze");
		btnUnfreeze.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				ArrayList<Object> send = new ArrayList<>();
				send.add("3");
				send.add("UnfreezeUser");
				int id=Integer.decode(textUserID.getText());
				send.add(id);
				mf.client.accept(send);
			}
		});
		btnUnfreeze.setBounds(357, 360, 110, 23);
		add(btnUnfreeze);
		
		JLabel lblUserid = new JLabel("UserID:");
		lblUserid.setBounds(153, 364, 72, 14);
		add(lblUserid);
		
		textUserID = new JTextField();
		textUserID.setBounds(237, 361, 110, 20);
		add(textUserID);
		textUserID.setColumns(10);
		
		JButton btnView = new JButton("View");
		btnView.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				init();
				ArrayList<Object> send = new ArrayList<>();
				send.add("3");
				send.add("ViewFrozenUsers");
				mf.client.accept(send);
				
			}
		});
		btnView.setBounds(495, 238, 89, 23);
		add(btnView);
		
		JLabel lblGraduationSystem = new JLabel("Graduation System");
		lblGraduationSystem.setFont(new Font("Segoe UI", Font.BOLD, 16));
		lblGraduationSystem.setBounds(226, 419, 157, 22);
		add(lblGraduationSystem);
		
		JLabel lblGraduatedUserid = new JLabel("Graduated UserID:");
		lblGraduatedUserid.setBounds(120, 465, 110, 14);
		add(lblGraduatedUserid);
		
		textGraduatedUser = new JTextField();
		textGraduatedUser.setBounds(237, 462, 105, 20);
		add(textGraduatedUser);
		textGraduatedUser.setColumns(10);
		
		JButton btnGraduate = new JButton("Graduate!");
		btnGraduate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<Object> send = new ArrayList<>();
				send.add("3");
				send.add("GraduateUser");
				int id=Integer.decode(textGraduatedUser.getText());
				send.add(id);
				mf.client.accept(send);
			}
		});
		btnGraduate.setBounds(357, 461, 110, 23);
		add(btnGraduate);
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(675, 634);
		add(picLabel);
	}
}
